package com.sinping.qh.api.app.version.v1.login;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.Update;
import com.sinping.qh.dto.app.user.UserByAppDTO;
import com.sinping.qh.service.app.login.ILoginService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 一、app登录时第一次生成token,并在redis存储key为token的userinfo信息.token保存到数据库
 * 二、第二次登录从数据库读取token,并且更新redis缓存的用户信息
 * 三、如果用户退出,只清除了redis的用户信息
 * 四、其它接口判断是否登录,只是根据redis缓存的用户信息是否为存在来判断
 *
 * @athor zhangjin
 * @create 2018/5/28
 */
@Api(value = "app登录", description = "app登录API")
@RestController
@RequestMapping(value = Const.BASE_URI, name = "app登录")
public class LoginEndPoint extends BaseAppEndPoint {

    private final static Logger logger = LoggerFactory.getLogger(LoginEndPoint.class);


    @Autowired
    ILoginService iLoginService;



    @ApiOperation(httpMethod = "PUT", value = "客户通过手机登录")
    @RequestMapping(value = "/applogin", name = "用户登录", method = RequestMethod.PUT)
    public ResDto login(@ApiParam(name="msg",value="{\"username\":\"zhangsan\",\"password\":\"000000\"}")
                            @RequestParam(required = true) String msg)
    {

        //验证传入参数是否正确
        UserByAppDTO userByAppDTO = JsonMapper.defaultMapper().fromJson(msg, UserByAppDTO.class);

        checkNull(userByAppDTO,ConstEum.PARAM_ERROR);

        checkValidator(userByAppDTO,Update.class,true);
        //去数据库验证登录,并生成token
        ResDto resDto=iLoginService.login(userByAppDTO);

        return resDto;
    }
}
